﻿#region 命名空间

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using WinnerCMS.BLL;
using WinnerCMS.Model;
using WinnerCMS.Common;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.Model
{
    [Access("257001,257002")]
    public partial class Add : WebFactory.PageBase
    {
        protected string SettingUploadPath = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["ID"] != null)
                {
                    this.btnOK.Text = "修改";
                    this.ddlBaseModel.Enabled = false;
                    Edit_Load();
                }
                else
                {
                    this.btnOK.Text = "添加";
                    InitFilter();
                }
            }
        }

        #region 添加或修改

        private void AddUI()
        {
            if (IsValid == false)
            {
                MsgBox.Message("页面数据验证发现错误。", this.UpdatePanel1);
                return;
            }


            var model = new WinnerCMS.Model.Model
            {
                ModelName = this.txtModelName.Text.Trim(),
                DataTable = this.txtDataTable.Text.Trim(),
                BaseModel = this.ddlBaseModel.SelectedValue,
                ContentName = this.txtContentName.Text.Trim(),
                ContentUnit = this.txtContentUnit.Text.Trim(),
                Remark = this.txtRemark.Text.Trim(),
                HtmlSavePath = this.txtHtmlSavePath.Text.Trim(),
                MaxCategoryPageNo = byte.Parse(this.NtxtMaxCategoryPageNo.Text),
                AdminUploadPath =
                    string.Format("{0}|{1}", this.RBL_AdminUploadPath.SelectedValue,
                        this.ddlAdminUploadPath.SelectedValue),
                AdminUploadSize = short.Parse(this.NtxtAdminUploadSize.Text),
                AdminFileTypes = this.txtAdminFileTypes.Text.Trim(),
                EnableMemberUpload = bool.Parse(this.RBL_EnableMemberUpload.SelectedValue),
                MemberUploadPath =
                    string.Format("{0}|{1}", this.RBL_MemberUploadPath.SelectedValue,
                        this.ddlMemberUploadPath.SelectedValue),
                MemberUploadSize = short.Parse(this.NtxtMemberUploadSize.Text),
                MemberFileTypes = this.txtMemberFileTypes.Text.Trim(),
                AdminPass = Convert.ToBoolean(this.RBL_AdminPass.SelectedValue),
                CommentAutoPass = Convert.ToBoolean(this.RBL_CommentAutoPass.SelectedValue),
                Contribution = bool.Parse(this.RBL_Contribution.SelectedValue),
                SearchOption = byte.Parse(this.RBL_SearchOption.SelectedValue),
                NewSignKeep = byte.Parse(this.NtxtNewSignKeep.Text),
                PageSize = byte.Parse(this.NtxtPageSize.Text)
            };

            //model.Filter = txtFilter.Text.Trim();


            if (model.NewSignKeep == 0)
            {
                model.IsShowNewSign = 0;
            }
            else
            {
                model.IsShowNewSign = 1;
            }

            if (Request["ID"] == null)
            {
                model.Id = 0;
            }
            else if (Request.QueryString["ID"] != null)
            {
                model.Id = byte.Parse(Request["ID"]);
            }

            var res = PolicyInjection.Create<ModelManage>().Add(model, this.DL_Filter);

            if (string.IsNullOrEmpty(res.Errors) == false)
            {
                MsgBox.Message(res.Errors, this.UpdatePanel1);
            }
            else
            {
                MsgBox.Message("操作成功！", "Model_Manage.aspx", this.UpdatePanel1);
            }
        }

        #endregion

        #region 加载

        private void Edit_Load()
        {
            var model = ModelManage.GetInfo(byte.Parse(Request["ID"]));
            if (model != null)
            {
                Title = "修改" + model.ModelName;

                this.txtModelName.Text = model.ModelName;
                this.txtDataTable.Text = model.DataTable;
                this.txtDataTable.Enabled = false;
                this.ddlBaseModel.SelectedValue = model.BaseModel;
                this.txtContentName.Text = model.ContentName;
                this.txtContentUnit.Text = model.ContentUnit;

                this.txtRemark.Text = model.Remark;
                this.txtHtmlSavePath.Text = model.HtmlSavePath;
                this.txtHtmlSavePath.Enabled = false;
                this.NtxtMaxCategoryPageNo.Text = model.MaxCategoryPageNo.ToString();

                var arr = model.AdminUploadPath.Split('|');
                if (arr.Length == 2)
                {
                    this.RBL_AdminUploadPath.SelectedValue = arr[0];

                    this.ddlAdminUploadPath.SelectedValue = arr[1];
                }


                this.NtxtAdminUploadSize.Text = model.AdminUploadSize.ToString();
                this.txtAdminFileTypes.Text = model.AdminFileTypes;
                this.RBL_EnableMemberUpload.SelectedValue = model.EnableMemberUpload.ToString();

                arr = model.MemberUploadPath.Split('|');
                if (arr.Length == 2)
                {
                    this.RBL_MemberUploadPath.SelectedValue = arr[0];

                    this.ddlMemberUploadPath.SelectedValue = arr[1];
                }


                this.NtxtMemberUploadSize.Text = model.MemberUploadSize.ToString();
                this.txtMemberFileTypes.Text = model.MemberFileTypes;
                this.RBL_AdminPass.SelectedValue = model.AdminPass.ToString();
                this.RBL_CommentAutoPass.SelectedValue = model.CommentAutoPass.ToString();
                this.RBL_Contribution.SelectedValue = model.Contribution.ToString();
                this.RBL_SearchOption.SelectedValue = model.SearchOption.ToString();
                this.NtxtPageSize.Text = model.PageSize.ToString();

                new ModelManage().Filter(model.Filter, this.DL_Filter);
            }
            else
            {
                //转向错误页面
                ErrClass.Redirect("数据不存在。");
            }
        }

        #endregion

        protected void btnOK_Click(object sender, EventArgs e)
        {
            AddUI();
        }

        protected void ddlBaseModel_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ddlBaseModel.SelectedValue == "Shop" || this.ddlBaseModel.SelectedValue == "Ask")
            {
                MsgBox.Message("该模型不能新建！", this.UpdatePanel1);
                return;
            }
            InitFilter();
        }

        private void InitFilter()
        {
            string str = null;

            if (this.ddlBaseModel.SelectedValue == "Soft")
            {
                str =
                    "版本|SoftVersion|True;大小|SoftSize|True;置顶|OnTop|True;推荐|Elite|True;演示地址|DemoUrl|True;软件作者|Author|True;软件开发商电邮|Email|True;软件开发商主页|Homepage|True;含图|IncludePic|True;缩略图|PicUrl|True;运行的系统|RunAtSystem|True;软件类型|SoftType|True;语言|SoftLanguage|True;授权类型|CopyrightType|True;等级|Stars|True;解压密码|DecompressPassword|True;更新时间|UpdateTime|True;最后点击时间|LastHitTime|True;流氓软件警告|BlackGuard|True;审核|Pass|True;浏览等级|Rank|True;点数|PointNum|True;颜色|ColorStyle|True;字体|FontStyle|True;允许评论|AllowComment|True;模板|TemplateFileName|True;幻灯片|IsSlide|True;稿件|IsDraft|True;";
            }
            if (this.ddlBaseModel.SelectedValue == "Article")
            {
                str =
                    "专题|SpecialID|True;文章小标题|Subtitle|True;摘要|Brief|True;文章作者|Author|True;文章来源|From|True;责任编辑|Editor|True;关键字|Key|True;点击次数|Hits|True;更新时间|UpdateTime|True;状态|ContentState|True;文章内容|Content|True;含图|IncludePic|True;缩略图链接|PicUrl|True;等级|Stars|True;等级|Rank|True;点数|PointNum|True;颜色|ColorStyle|True;字体|FontStyle|True;允许评论|AllowComment|True;模板|TemplateFileName|True;幻灯片|IsSlide|True;";
            }

            if (this.ddlBaseModel.SelectedValue == "Photo")
            {
                str =
                    "专题|SpecialID|True;价格|PhotoPrice|True;规格|Standard|True;包装|Packaging|True;商标|Brand|True;置顶|OnTop|True;推荐|Elite|True;含图|IncludePic|True;缩略图|PicUrl|True;大图|BigPicUrl|True;等级|Stars|True;更新时间|UpdateTime|True;审核|Pass|True;浏览等级|Rank|True;点数|PointNum|True;颜色|ColorStyle|True;字体|FontStyle|True;允许评论|AllowComment|True;模板|TemplateFileName|True;幻灯片|IsSlide|True;稿件|IsDraft|True;";
            }

            if (string.IsNullOrEmpty(str) == false)
                new ModelManage().Filter(str, this.DL_Filter);
        }
    }
}